#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef struct
{
    int* arr;
    int front;
    int rear;
    int k;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k)
{
    MyCircularQueue* pst = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    pst->arr = (int*)malloc(sizeof(int) * (k + 1));
    pst->front = 0;
    pst->rear = 0;
    pst->k = k;
    return pst;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj)
{
    return obj->front == obj->rear;
}

bool myCircularQueueIsFull(MyCircularQueue* obj)
{
    return ((obj->rear + 1) % (obj->k + 1) == obj->front);
}


bool myCircularQueueEnQueue(MyCircularQueue* obj, int value)
{
    if (myCircularQueueIsFull(obj))
        return false;
    obj->arr[obj->rear] = value;

    obj->rear++;
    obj->rear %= (obj->k + 1);
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj)
{
    if (myCircularQueueIsEmpty(obj))
        return false;
    obj->front++;
    obj->front %= (obj->k + 1);
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj)
{
    if (myCircularQueueIsEmpty(obj))
        return -1;
    return obj->arr[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj)
{
    if (myCircularQueueIsEmpty(obj))
        return -1;
    return obj->arr[(obj->rear + obj->k) % (obj->k + 1)];
}


void myCircularQueueFree(MyCircularQueue* obj)
{
    free(obj->arr);
    free(obj);
}

/**
 * Your MyCircularQueue struct will be instantiated and called as such:
 * MyCircularQueue* obj = myCircularQueueCreate(k);
 * bool param_1 = myCircularQueueEnQueue(obj, value);

 * bool param_2 = myCircularQueueDeQueue(obj);

 * int param_3 = myCircularQueueFront(obj);

 * int param_4 = myCircularQueueRear(obj);

 * bool param_5 = myCircularQueueIsEmpty(obj);

 * bool param_6 = myCircularQueueIsFull(obj);

 * myCircularQueueFree(obj);
*/
int main()
{
    MyCircularQueue* obj = myCircularQueueCreate(2);
    myCircularQueueEnQueue(obj, 2);
    myCircularQueueEnQueue(obj, 1);
    myCircularQueueDeQueue(obj);
    myCircularQueueEnQueue(obj, 3);
    myCircularQueueDeQueue(obj);
    myCircularQueueEnQueue(obj, 3);
    myCircularQueueDeQueue(obj);
    myCircularQueueEnQueue(obj, 3);
    myCircularQueueDeQueue(obj);
    printf("%d",myCircularQueueFront(obj));
    myCircularQueueFree(obj);
    return 0;

}